package com.fone.player.storage.download;

import java.util.List;

import android.app.NotificationManager;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;

import com.fone.player.entity.Download;
import com.fone.player.storage.FileDownloadDataBaseAdapter;
import com.fone.player.util.L;

public class MediaReceiver extends BroadcastReceiver {
	private static final String TAG = MediaReceiver.class.getSimpleName();

	@Override
	public void onReceive(Context context, Intent intent) {
		String action = intent.getAction();
		L.v(TAG, "onReceive", "start action=" + action);
		if (Intent.ACTION_MEDIA_REMOVED.equals(action)
				|| Intent.ACTION_MEDIA_UNMOUNTED.equals(action)
				|| Intent.ACTION_MEDIA_BAD_REMOVAL.equals(action)
				|| Intent.ACTION_MEDIA_EJECT.equals(action)
				|| Intent.ACTION_MEDIA_SHARED.equals(action)
				|| Intent.ACTION_MEDIA_UNMOUNTABLE.equals(action)) {
			// SD卡挂载失败
			L.e(TAG, "onReceive", "storage mounted fail");
			// 清理之前的通知栏
			List<Download> downloadList = FileDownloadDataBaseAdapter
					.getInstance().getDownloadList(Download.DOWNLOAD_STATE_ALL);
			if (downloadList != null) {
				NotificationManager notificationManager = (NotificationManager) context
						.getSystemService(Context.NOTIFICATION_SERVICE);
				for (Download download : downloadList) {
					notificationManager.cancel(download
							.getDownloadNotificationId());
				}
			}
		}

	}

}
